package com.example.nick.bodyapplication.notification

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.RemoteViews
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.example.nick.bodyapplication.GreenDaoActivity
import com.example.nick.bodyapplication.R
import com.example.nick.bodyapplication.number.NumberActivity

class NotifcationActivity : AppCompatActivity() {

    lateinit var ctx: Context

    val notificationId = 123
    val CHANNEL_ID = "test_notification"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notifcation)
        ctx = this
    }

    fun notification1(view: View) {
        var builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.auto_temp)
            .setContentTitle("textTitle")
            .setContentText("textContent")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

//        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//
//        // Since android Oreo notification channel is needed.
//        //需要在创建CHANNEL时确定
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            val channel = NotificationChannel(
//                CHANNEL_ID,
//                "Channel human readable title",
//                NotificationManager.IMPORTANCE_DEFAULT
//            )
////            if (TextUtils.isEmpty(notification.getSound())) {
////                channel = NotificationChannel(
////                    CHANNEL_ID,
////                    "Channel human readable title",
////                    NotificationManager.IMPORTANCE_LOW
////                )
////            } else {
////                channel = NotificationChannel(
////                    CHANNEL_ID,
////                    "Channel human readable title",
////                    NotificationManager.IMPORTANCE_DEFAULT
////                )
////            }
//            notificationManager.createNotificationChannel(channel)
//        } else {
////            if (TextUtils.isEmpty(notification.getSound())) {
////                notificationBuilder.setSound(null)
////            } else {
////                notificationBuilder.setSound(defaultSoundUri)
////            }
//        }

//        notificationManager.notify(notificationId, builder.build())
        notifyAll(notificationId, builder.build())

    }

    fun notification2(view: View) {
        val snoozeIntent = Intent(this, GreenDaoActivity::class.java).apply {
            action = "myCall"
//            putExtra(EXTRA_NOTIFICATION_ID, 0)
        }
        val myNumberIntent = Intent(this, NumberActivity::class.java).apply {
            action = "myNumber"
//            putExtra(EXTRA_NOTIFICATION_ID, 0)
        }
        val snoozePendingIntent: PendingIntent =
            PendingIntent.getActivity(this, 0, snoozeIntent, PendingIntent.FLAG_ONE_SHOT)
        val myNumberPendingIntent: PendingIntent =
            PendingIntent.getActivity(this, 0, myNumberIntent, PendingIntent.FLAG_ONE_SHOT)
        val builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.airconditioner)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(myNumberPendingIntent)
            .addAction(
                R.drawable.icon_turndown, "hangup",
                snoozePendingIntent
            )
            .addAction(
                R.drawable.icon_answer, "answer",
                snoozePendingIntent
            )
        notifyAll(22, builder.build())
    }

    private fun createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "my_channel_name"
            val descriptionText = "my_channel_description"
            val importance = NotificationManager.IMPORTANCE_HIGH
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }


    fun notifyAll(channelId: Int, notification: Notification) {
        createNotificationChannel()
        with(NotificationManagerCompat.from(this)) {
            // notificationId is a unique int for each notification that you must define
            notify(channelId, notification)
        }
    }

    @SuppressLint("RemoteViewLayout")
    fun notification3(view: View) {
        // Apply the layouts to the notification
        val intent = Intent(this, GreenDaoActivity::class.java).apply {
            action = "myNumber"
        }
        startActivity(intent)
        intent.putExtra("Btn_value", 1)
        val pendingIntentMain = PendingIntent.getActivity(
            this,
            11 /* Request code */,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        intent.putExtra("Btn_value", 2)
        val pendingIntentHangup = PendingIntent.getActivity(
            this,
            22 /* Request code */,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        intent.putExtra("Btn_value", 3)
        val pendingIntentAnswer = PendingIntent.getActivity(
            this,
            33 /* Request code */,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        )

//        val intentBroad = PendingIntent.getBroadcast(ctx, 0, Intent("my_broadcast_number").apply {
//            setClass(ctx, MyReceiver::class.java)
//            putExtra("Btn_value", 444)
//        }, PendingIntent.FLAG_UPDATE_CURRENT)
        val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
        notificationLayout.setTextViewText(R.id.tvTitle, "doorbell")
        notificationLayout.setTextViewText(R.id.tvContent, "body calling")
        notificationLayout.setOnClickPendingIntent(R.id.ivHangup, pendingIntentHangup)//pendingIntentHangup)
        notificationLayout.setOnClickPendingIntent(R.id.ivAnswer, pendingIntentAnswer)

//        val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)
        val vibrate = longArrayOf(0, 500, 1000, 1500)
        // Apply the layouts to the notification
        val customNotification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notify_ic)
//            .setContentTitle("2doorbell")
//            .setContentText("2body calling")
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntentMain)
//            .setSound(Uri.fromFile(File("/system/media/audio/ringtones/Luna.ogg"))) //设置通知提示音
            .setVibrate(vibrate)
//            .setFullScreenIntent(pendingIntentMain, true) // 需要打开在屏幕端显示选项，避免勿打扰
            .setAutoCancel(true)
//            .setCustomBigContentView(notificationLayoutExpanded)
//            .setStyle(NotificationCompat.BigPictureStyle().bigLargeIcon(null))
            .build()

//        notifyAll(56, customNotification)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "my_channel_name"
            val descriptionText = "my_channel_description"
            val importance = NotificationManager.IMPORTANCE_HIGH // 弹出式通知，需要手机打开app对应通知允许弹出
            val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
        with(NotificationManagerCompat.from(this)) {
            // notificationId is a unique int for each notification that you must define
            notify(56, customNotification)
        }
    }
}